package com.entrogy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.*;
import com.entrogy.dao.SalarySlipMapper;
import com.entrogy.dao.UserMapper;
import com.entrogy.service.SalarySlipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName SalarySlipServiceImpl
 * @Description
 * @Author luoxingjiang
 * @Date 2019/8/27 0027 下午 3:03
 * @Version 1.0
 **/
@Service
@Transactional
public class SalarySlipServiceImpl implements SalarySlipService {

    @Autowired
    private SalarySlipMapper salarySlipMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean insertSalarySlipTemplate(SalarySlipTemplateEntity salarySlipTemplate) {
        Integer schoolCode = userMapper.getSchoolCodeByUserId(salarySlipTemplate.getCreateUserId());
        salarySlipTemplate.setSchoolCode(schoolCode);
        //首先插入模板信息
        int insertSalarySlipTemplateRow = salarySlipMapper.insertSalarySlipTemplate(salarySlipTemplate);
        List<SalaryComposeEntity> salaryTemplateDataList = salarySlipTemplate.getSalaryComposeList();
        for (SalaryComposeEntity salaryTemplateData : salaryTemplateDataList) {
            //插入工资条组成信息
            int row = salarySlipMapper.insertSalarySlipTemplateData(salaryTemplateData.getSalaryComposeName(), salarySlipTemplate.getId());
        }
        return true;
    }

    @Override
    public boolean updateSalarySlipTemplate(SalarySlipTemplateEntity salarySlipTemplate) {
        // 首先更新模板信息
        int updateSalarySlipTemplateRow = salarySlipMapper.updateSalarySlipTemplate(salarySlipTemplate);
        if (salarySlipTemplate.getSalaryComposeList().size() > 0){
            // 如果需要更新工资条的组成信息的话
            // 首先删除该工资条的组成信息---这里不能删除，只能改未启用状态，因为修改后，可能历史工资条还在使用
//            int deleteSalaryCompose = salarySlipMapper.deleteSalaryTemplateCompose(salarySlipTemplate.getId());
            // 启用状态为0表示禁用
            int enable = 0;
            int updateSalaryComposeRow = salarySlipMapper.updateSalaryTemplateCompose(salarySlipTemplate.getId(), enable);
            List<SalaryComposeEntity> salaryTemplateDataList = salarySlipTemplate.getSalaryComposeList();
            // 然后再插入新的工资条组成信息
            for (SalaryComposeEntity salaryTemplateData : salaryTemplateDataList) {
                // 插入这个模板包含哪些字段
                int row = salarySlipMapper.insertSalarySlipTemplateData(salaryTemplateData.getSalaryComposeName(), salarySlipTemplate.getId());
            }
        }
        return true;
    }

    @Override
    public List<SalarySlipTemplateEntity> listSalarySlipTemplate(Integer templateId, Integer enableStatus) {
        return salarySlipMapper.listSalarySlipTemplate(templateId, enableStatus);
    }

    @Override
    public boolean initSalaryData(List<JSONObject> userList, SalarySlipTemplateEntity salarySlipTemplate, Integer salaryYear, Integer salaryMonth) {
        for (JSONObject user : userList) {
            String userId = user.getString("id");
            SalaryTeacherInfoEntity salaryTeacherInfoEntity = new SalaryTeacherInfoEntity();
            salaryTeacherInfoEntity.setSalaryYear(salaryYear).setSalaryMonth(salaryMonth).setTeacherId(userId);
            //首先插入工资条状态信息，信息状态为未读并且为发送，插入后会返回该条工资条的id即salaryTeacherInformationId
            int insertSalaryInformationRow = salarySlipMapper.insertSalaryInformation(salaryTeacherInfoEntity);
            //将员工的工资条的各项值初始化为null
            int insertTeacherSalaryRow = salarySlipMapper.insertTeacherSalary(userId, salarySlipTemplate.getSalaryComposeList(), salaryTeacherInfoEntity.getId());
        }
        return true;
    }

    @Override
    public List<SalaryTeacherInfoEntity> listTeacherSalaryInfo(Integer salaryYear, Integer salaryMonth, Integer salaryInfoId, Integer schoolCode) {
        return salarySlipMapper.listTeacherSalaryInfo(salaryYear, salaryMonth, salaryInfoId, schoolCode);
    }

    @Override
    public List<SalaryTeacherInfoEntity> ListTeacherSalarySlipId(Integer salaryYear, Integer salaryMonth, String teacherId, Integer send, Integer readStatus, Integer confirmStatus, Integer recallStatus) {
        return salarySlipMapper.ListTeacherSalarySlipId(salaryYear, salaryMonth, teacherId, send, readStatus, confirmStatus, recallStatus);
    }

    @Override
    public boolean insertTeacherSalary(SalaryTeacherDetailEntity salaryTeacherDetail) {
        return salarySlipMapper.insertTeacherSalaryDetail(salaryTeacherDetail) == 1;
    }

    @Override
    public boolean deleteTeacherSalaryInfo(Integer salaryTeacherInformationId) {
        return salarySlipMapper.deleteTeacherSalaryInfo(salaryTeacherInformationId) == 1;
    }

    @Override
    public boolean insertSalaryInformation(SalaryTeacherInfoEntity salaryTeacherInfoEntity) {
        int insertSalaryInformationRow = salarySlipMapper.insertSalaryInformation(salaryTeacherInfoEntity);
        return insertSalaryInformationRow == 1;
    }

    @Override
    public Integer getSalarySlipTemplateIdBySchoolCode(Integer schoolCode) {
        return salarySlipMapper.getSalarySlipTemplateIdBySchoolCode(schoolCode);
    }

    @Override
    public List<SalaryTeacherInfoEntity> listTeacherSalarySlipInfo(Integer schoolCode, Integer salaryYear, Integer salaryMonth, String userId) {
        return salarySlipMapper.listTeacherSalarySlipInfo(schoolCode, salaryYear, salaryMonth, userId);
    }

    @Override
    public List<MonthSalaryStatusEntity>  listMonthSalaryStatus(Integer schoolCode) {
        // 查看该学校有哪些月份的工资条
        List<MonthSalaryStatusEntity> monthSalaryStatusList = salarySlipMapper.listMonthSalary(schoolCode);
        for (MonthSalaryStatusEntity monthSalaryStatus : monthSalaryStatusList) {
            Integer salaryYear = monthSalaryStatus.getSalaryYear();
            Integer salaryMonth = monthSalaryStatus.getSalaryMonth();
            // 查询总条数
            Integer salarySlipTotalCount = salarySlipMapper.countTotalSalarySlip(salaryYear, salaryMonth, schoolCode);
            // 查询未读条数
            Integer hasNotRead = salarySlipMapper.countHasNotRead(salaryYear, salaryMonth, schoolCode);
            // 查询未发送条数
            Integer hasNotSend = salarySlipMapper.countHasNotSend(salaryYear, salaryMonth, schoolCode);
            // 查询未确认条数
            Integer hasNotConfirm = salarySlipMapper.countHasNotConfirm(salaryYear, salaryMonth, schoolCode);
            monthSalaryStatus.setSalarySlipTotalCount(salarySlipTotalCount)
                    .setHasNotRead(hasNotRead)
                    .setHasRead(salarySlipTotalCount - hasNotRead)
                    .setHasNotSend(hasNotSend)
                    .setHasSend(salarySlipTotalCount - hasNotSend)
                    .setHasNotConfirm(hasNotConfirm)
                    .setHasConfirm(salarySlipTotalCount - hasNotConfirm);
        }
        return monthSalaryStatusList;
    }

    @Override
    public boolean updateSalaryInfoStatus(Integer send, Integer readStatus, Integer confirmStatus, Integer recallStatus, Integer salaryInfoId) {
        return salarySlipMapper.updateSalaryInfoStatus(send, readStatus, confirmStatus, recallStatus, salaryInfoId) == 1;
    }

    @Override
    public Integer countTeacherSalarySlip(Integer schoolCode, Integer salaryYear, Integer salaryMonth, String userId) {
        return salarySlipMapper.countTeacherSalarySlip(schoolCode, salaryYear, salaryMonth, userId);
    }

    @Override
    public List<String> listPhoneBySalaryInfoId(List<Integer> salaryInfoIdList) {
        return salarySlipMapper.listPhoneBySalaryInfoId(salaryInfoIdList);
    }

    @Override
    public boolean updateSalaryDetail(UpdateTeacherSalaryEntity teacherSalaryEntity) {
        List<SalaryTeacherDetailEntity> salaryTeacherDetailList = teacherSalaryEntity.getSalaryTeacherDetailList();
        int totalUpdateRow = 0;
        for (SalaryTeacherDetailEntity salaryTeacherDetail : salaryTeacherDetailList) {
            int row = salarySlipMapper.updateSalaryDetail(salaryTeacherDetail.getId(), salaryTeacherDetail.getValue());
            if (row > 0){
                totalUpdateRow++;
            }
        }
        return totalUpdateRow == salaryTeacherDetailList.size();
    }

    @Override
    public Integer countMonthSalary(Integer schoolCode) {
        return salarySlipMapper.countMonthSalary(schoolCode);
    }

    @Override
    public boolean insertBatchSalary(List<SalaryTeacherDetailEntity> salaryTeacherDetailList) {
        int insertBatchSalaryRow = salarySlipMapper.insertBatchSalary(salaryTeacherDetailList);
        return false;
    }

    @Override
    public List<Integer> listMonthSalaryInfoId(Integer salaryYear, Integer salaryMonth, Integer schoolCode) {
        return salarySlipMapper.listMonthSalaryInfoId(salaryYear, salaryMonth, schoolCode);
    }

    @Override
    public boolean deleteTeacherSalaryDetail(Integer salaryTeacherInformationId) {
        salarySlipMapper.deleteTeacherSalaryDetail(salaryTeacherInformationId);
        return true;
    }
}
